package cn.manjuu.touhou.activity.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import cn.manjuu.touhou.APP;
import cn.manjuu.touhou.R;
import cn.manjuu.touhou.engine.NavigationEngine;
import cn.manjuu.touhou.util.Looger;

public class DiTuFragment extends Fragment implements OnTouchListener,
		OnClickListener {

	private RelativeLayout mMainView;
	private ImageView iv_map;
	private ImageView iv_hong_mo_guan;
	private RelativeLayout rl_map;
	private AbsoluteLayout al_map;
	private int startX;
	private int startY;
	boolean isMove = false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mMainView = (RelativeLayout) inflater.inflate(R.layout.di_tu, null);
		findView();
		registerListener();
		return mMainView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		init();
	}

	private void init() {
	}

	private void viewGone() {
	}

	private void findView() {
		iv_map = (ImageView) mMainView.findViewById(R.id.iv_map);
		iv_hong_mo_guan = (ImageView) mMainView
				.findViewById(R.id.iv_hong_mo_guan);
		rl_map = (RelativeLayout) mMainView.findViewById(R.id.rl_map);
		al_map = (AbsoluteLayout) mMainView.findViewById(R.id.al_map);

	}

	private void registerListener() {
		// TODO Auto-generated method stub
		iv_hong_mo_guan.setOnClickListener(this);
		iv_hong_mo_guan.setOnTouchListener(this);
		rl_map.setOnTouchListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_hong_mo_guan:
			NavigationEngine.getInstance().toDaramActivity("1");
			break;
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		int action = event.getAction() & MotionEvent.ACTION_MASK;
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			startX = (int) event.getRawX();
			startY = (int) event.getRawY();
			break;
		case MotionEvent.ACTION_MOVE:
			isMove = true;

			int dxx = (int) (event.getRawX() - startX);
			int dyy = (int) (event.getRawY() - startY);

			int l = (int) (al_map.getLeft() + dxx);
			int t = (int) (al_map.getTop() + dyy);
			int r = al_map.getRight();
			int b = al_map.getBottom();
			// Looger.i(APP.DiTuFragment, "l: " + l + ", r: " + r + ", t: " + t
			// + ", b: " + b);
			if (l > 240) {
				l = 240;
			}
			if (l < -900) {
				l = -900;
			}
			if (t > 400) {
				t = 400;
			}
			if (t < -300) {
				t = -300;
			}
			al_map.layout(l, t, r, b);

			startX = (int) event.getRawX();
			startY = (int) event.getRawY();
			break;
		case MotionEvent.ACTION_UP:
			if (!isMove) {
				v.performClick();
			}
			isMove = false;
			break;
		}
		return true;
	}
}
